<?php

class NguoiDungTaiKhoanController extends BaseController {

    protected $tai_khoan;

    public function __construct(Tai_khoan $tai_khoan) {
        $this->tai_khoan = $tai_khoan;
    }

    public function postLogin() {
        $user = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
        if (Auth::attempt($user)) {
            return '1';
        } else {
            return '0';
        }
    }

    public function getLogout() {
        Auth::logout();
        return Redirect::to('/')->with('error', 'Đăng xuất thành công!!!');
        ;
    }

    public function getChangepass() {
        Return View::make('nguoidungtaikhoan.changepassword');
    }

    public function postChangepass() {
        $input = Input::all();
        $user = Auth::user();
        if (!Hash::check($input['old_password'], $user->password)) {
            return '0';
        } else {
            $user->password = Hash::make(Input::get('new_password'));
            $user->save();
            return '1';
        }
    }

    public function getInfouser() {
        $tai_khoan = $this->tai_khoan->find(Auth::user()->id);
        if(Auth::user()->auth_type == 'student'){
            return View::make('nguoidungtaikhoan.thongtinhocvien', compact('tai_khoan'));
        }
        else{
            return View::make('nguoidungtaikhoan.thongtincanhan', compact('tai_khoan'));
        }
    }

    public function getAdministrator() {
        $tai_khoan = $this->tai_khoan->find(Auth::user()->id);
        return View::make('pages.trangquantri', compact('tai_khoan'));
    }

    public function postInfouser() {
        $input = Input::all();
        DB::table('tai_khoans')->where('id', Auth::user()->id)->update($input);
        return 1;
    }

    

    public function getCreateuser()
    {
        return View::make('nguoidungtaikhoan.create');
    }

    public function postCreateuser(){
        $id = 0;
        $input = Input::all();
        $password_old = $input['password'];
        $input['password'] = Hash::make($input['password']);
        $input['auth_type'] = 'student';
        $input['active'] = 0;
        $validation = Validator::make($input, Tai_khoan::$rules_student);
        if($validation->passes()){
                $id = DB::table('tai_khoans')->insertGetId($input);
            if($id != 0 ){
                Auth::attempt(array('username'=>$input['username'],'password'=>$password_old));
                return $id;    
            }else{
                return 0;
            }
        }
        return 0;
        
    }

    public function getLoginWithFacebook() {

        // get data from input
        $code = Input::get( 'code' );

        // get fb service
        $fb = OAuth::consumer( 'Facebook' );

        // check if code is valid

        // if code is provided get user data and sign in
        if ( !empty( $code ) ) {

            // This was a callback request from facebook, get the token
            $token = $fb->requestAccessToken( $code );

            // Send a request with it
            $result = json_decode( $fb->request( '/me' ), true );

            $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
            echo $message. "<br/>";

            //Var_dump
            //display whole array().
            dd($result);

        }
        // if not ask for permission first
        else {
            // get fb authorization
            $url = $fb->getAuthorizationUri();

            // return to facebook login url
             return Redirect::to( (string)$url );
        }

        }

    public function getLoginWithGoogle() {

        // get data from input
        $code = Input::get( 'code' );

        // get google service
        $googleService = OAuth::consumer( 'Google' );

        // check if code is valid

        // if code is provided get user data and sign in
        if ( !empty( $code ) ) {

            // This was a callback request from google, get the token
            $token = $googleService->requestAccessToken( $code );

            // Send a request with it
            $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true );

            $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
            echo $message. "<br/>";

            //Var_dump
            //display whole array().
            dd($result);

        }
        // if not ask for permission first
        else {
            // get googleService authorization
            $url = $googleService->getAuthorizationUri();

            // return to google login url
            return Redirect::to( (string)$url );
        }
    }
}
